# -*- mode: python; -*-

import config_opts
import os, os.path

def check_config(env):
    # Check configuration
    req_headers = { 'boost/version.hpp'         : 'boost',
                    'ao/ao.h'                   : 'ao',
                    'ogg/ogg.h'                 : 'ogg',
                    'google/protobuf/message.h' : 'protobuf',
                    'sqlite3.h'                 : 'sqlite3',
                    'vorbis/vorbisfile.h'       : 'vorbis'}
    conf = Configure(env)
    if not env.GetOption('clean'):
        for (hdr, lib) in req_headers.items():
            if not conf.CheckCXXHeader(hdr):
                print '%s must be installed!' % lib
                Exit(1)

env = Environment(ENV={'LD_LIBRARY_PATH' : os.getenv('LD_LIBRARY_PATH'),
                       'PATH' : os.getenv('PATH')},
                  options=config_opts.get_options())
Export('env')

env.AppendUnique(CPPPATH=['$AO_INCLUDE_DIR',
                          '$BOOST_INCLUDE_DIR',
                          '$OGG_INCLUDE_DIR',
                          '$PROTOBUF_INCLUDE_DIR',
                          '$SQLITE3_INCLUDE_DIR',
                          '$VORBIS_INCLUDE_DIR',
                          '.'])

# env.Append(CXXFLAGS=['-O0', '-g'])
env.Append(CXXFLAGS=['/EHsc'])

check_config(env)

# sources in this directory
lib_sources = [
    'audio_source_factory.cpp',
    'library.cpp',
    'player.cpp',
    'queue.cpp',
    'queue_cursor.cpp',
    'queue_source.cpp',
    'source_descriptor.cpp'
]

# subdirectories
subdirs = [
    'audio_sinks', 
    'audio_sources', 
    'sqlite3pp']

for subdir in subdirs:
    lib_sources.append(SConscript(os.path.join(subdir, 'SConscript')))

lib_sources.append(SConscript(os.path.join('protobuf', 'SConscript'))[0])

# build pita library
lib = env.SharedLibrary('#/lib/pita', lib_sources)

# build pita2 daemon
binenv = env.Copy()
binenv.Append(LIBPATH=['$BOOST_LIB_DIR',
                       '$AO_LIB_DIR',
                       '$PROTOBUF_LIB_DIR'])
binenv.Append(LIBS=[lib,
                    'ao', 
                    'boost_filesystem',
                    'boost_signals', 
                    'boost_thread', 
                    'protobuf',
                    'sqlite3', 
                    'vorbisfile'])
bin_sources = [
    'main.cpp',
    'config/config_parser.cpp',
    'network/network_interface.cpp'
]
bin = binenv.Program('#/bin/pita2', bin_sources)

# build libtest
libtest = binenv.Program('#/bin/libtest', ['libtest.cpp'])

Default([bin, libtest])
